package edu.hdu.android.sudoku;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.Toast;

public class Sudoku extends AppCompatActivity implements View.OnClickListener {

    private static final String TAG = "Sudoku";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button aboutGame = findViewById(R.id.about_game);
        Button newGame = findViewById(R.id.new_game);
        Button loadGame = findViewById(R.id.load_game);
        Button exitGame = findViewById(R.id.exit_game);
        aboutGame.setOnClickListener(this);
        newGame.setOnClickListener(this);
        loadGame.setOnClickListener(this);
        exitGame.setOnClickListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.music:
                if(MyApplication.isRelease){
                    Toast.makeText(this, "播放音乐", Toast.LENGTH_SHORT)
                            .show();
                    MyApplication.mPlayer = MediaPlayer.create(this, R.raw.music);
                    MyApplication.mPlayer.start();   //开始播放
                    MyApplication.isRelease = false;
                } else {
                    Toast.makeText(this, "关闭音乐", Toast.LENGTH_SHORT)
                            .show();
                    MyApplication.mPlayer.reset();     //重置MediaPlayer
                    MyApplication.mPlayer.release();   //释放MediaPlayer
                    MyApplication.isRelease = true;
                }
                return true;
            case R.id.hints:
                Toast.makeText(this, "请开始游戏", Toast.LENGTH_SHORT).show();
                return true;
            default: break;
        }
        return false;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.about_game:
                AlertDialog.Builder dialog = new AlertDialog.Builder(this);
                dialog.setTitle(R.string.about_title);
                dialog.setMessage(R.string.about_text);
                dialog.setCancelable(false);
                dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                    }
                });
                dialog.show();
                break;
            case R.id.new_game:
                openNewGameDialog();
                break;
            case R.id.load_game:
                openLoadGameDialog();
                break;
            case R.id.exit_game:
                finish();
                break;
        }
    }

    private void openNewGameDialog() {
        Toast.makeText(this, "请选择游戏难度", Toast.LENGTH_SHORT).show();
        new AlertDialog.Builder(this)
                .setTitle(R.string.new_game)
                .setItems(R.array.difficulty, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        startGame(i);
                    }
                }).show();
    }

    private void openLoadGameDialog() {
        Toast.makeText(this, "请选择游戏难度", Toast.LENGTH_SHORT).show();
        new AlertDialog.Builder(this)
                .setTitle(R.string.load_game)
                .setItems(R.array.difficulty, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        loadGame(i);
                    }
                }).show();
    }

    private void startGame(int i) {
        MyApplication.difficulty = i;
        MyApplication.flag = 0;
        startActivity(new Intent(this, GameActivity.class));
    }

    private void loadGame(int i) {
        MyApplication.difficulty = i;
        MyApplication.flag = i + 1;//载入第几难度存档
        startActivity(new Intent(this, GameActivity.class));
    }
}
